const app = getApp();
const companyName = app.globalData.companyName;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coachList: [],
    page: 1,
    pageSize: 20
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const adminLogin = app.globalData.adminLogin;
    if (adminLogin === false) {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      wx.navigateTo({
        url: '/pages/login/login',
      });
    }else{
      this.setData({
        page: 1,
        pageSize: 20,
        coachList: []
      });1234
      this.fatchCoachList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  fatchCoachList: function() {
    const that = this;
    const coachList = that.data.coachList;
    const url = app.globalData.url;
    const page = that.data.page;
    const pageSize = that.data.pageSize;
    wx.showNavigationBarLoading();
    wx.request({
      url: url + '/management/queryCoach',
      data: {
        companyName,
        page,
        pageSize
      },
      method: 'POST',
      success: function(response) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (response.data.success) {
          const content = response.data.content;
          if (content && content.length > 0) {
            content.forEach(function(item) {
              coachList.push(item);
            });
            that.setData({
              coachList
            });
          } else {
            wx.showToast({
              title: '没有更多数据了~',
              icon: 'none'
            })
          }
        }else{
          wx.showToast({
            title: response.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      pageSize: 20,
      coachList: []
    });
    this.fatchCoachList();
  },
  edit: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.setStorageSync('id', id);
    wx.navigateTo({
      url: '/pages/detail/detail'
    })
  },
  deleteById: function(e) {
    const id = e.currentTarget.dataset.id;
    const that = this;
    const url = app.globalData.url;
    wx.showModal({
      title: '提示',
      content: '确定删除此教练？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: url + '/coach/delete/' + id,
            method: 'GET',
            success: function(response) {
              if (response.data.success) {
                that.onPullDownRefresh();
              } else {
                wx.showToast({
                  title: response.data.message || '删除失败',
                  icon: 'none'
                })
              }
            }
          });
        }
      }
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const page = this.data.page;
    this.setData({
      page: page + 1,
      pageSize: 20
    });
    this.fatchCoachList();
  },
  addCoach: function() {
    wx.navigateTo({
      url: '/pages/addCoach/addCoach',
    })
  }
})